<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\DB;

class CustomeRank extends Model
{
   protected $table = 'ordes';



    public function user()
    {
        return $this->belongsTo(User::class,'user_id');
    }



    public function paginate()
    {

        $perPage = Request::get('per_page', 10);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;


        $result = DB::table('orders')
            //->join('users','orders.seller_id','=','users.id')
            ->groupBy('user_id','user_name')
            ->orderByDesc('profit')
            ->get(
                array(
                    DB::raw('SUM(total_amount) as profit'),
                    DB::raw('user_id'),
                    DB::raw('user_name'),
                )
            )->toArray();


        $movies = static::hydrate($result);

        $paginator = new LengthAwarePaginator($movies, $start, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }
}
